// Copyright (c) 2010, Anthony Cassidy
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are 
// permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice, this list 
//         of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice, this 
//         list of conditions and the following disclaimer in the documentation and/or 
//         other materials provided with the distribution.
//     * Neither the name of the AntsAdventureGameCreator nor the names of its contributors 
//         may be used to endorse or promote products derived from this software without 
//         specific prior written permission.
// 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
// THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <deque>
#include "XReceiver.h"
#include "XFrameAnimationKeyword.h"
#include "Ximage.h"
#include "RoomObject.h" //for special animation
class XAnimations;
class IXMainWindowMethodsXRoomNeeds;


class XRoomObject : public XReceiver
{
public:
    XRoomObject(QString keyword, IXMainWindowMethodsXRoomNeeds& pCallback);
    ~XRoomObject();

    void PlayAnimation(QString animKeyword, int delay=0, int isRepeat=0, int isBackward=0,int holdLastFrame=0);
    void SetVisible(bool visible = true);
    bool IsVisible();
    void DrawIfVisible(QPainter& displayPainter, QPainter& zbufferPainter, QRgb zbufferColor, bool isDotMode, bool isBoxMode);
    void DrawSpeechIfVisible(QPainter& painter);
    void SetX(int x);
    void SetY(int y);
    void SetParentWidget(QWidget* widget);
    void SetChoicesResult(int result);

    int X();
    int Y();

    void SetCurrentAnimationName(QString animKeyword);
    QString CurrentAnimName();
    void SetCurrentAnimationFrameIndex(int frame);
    int GetCurrentAnimationFrameIndex() const;

    QString GetDisplayName();
    void SetDisplayName(QString displayName);


    void SetFullPath(QString subFolder); // non trivial
    void AddAnimation(QString animKeyword, QString fullPath);

    QString Keyword() const;

    void SetCurrentImageFilename(QString filename);

    QPointer<XAnimations>& Animations();

    void timerEvent(QTimerEvent* timer);

    void Say(QString speech);
    void SayWithoutAnimation(QString speech);
    void SetTalkingAnimationName(QString str);
    QString GetSpeech();

    void SetIndex(int i);// method needed for ZBuffer color
    void IncrementFrame();
    void DecrementFrame();
    void GotoFirstFrame();
    void GotoLastFrame();
    void PreloadAnimation(QString animKeyword);
    void SetTalkingDelay(int i);
    void PlayAnimationHoldLastFrame(QString animKeyword, int delay = 0);
    void PlayAnimationRepeatWhilstVisible(QString animKeyword, int delay = 0);


    void PlayAnimationBackwards(QString,int);

    void PlayAnimationBackwardsHoldLastFrame(QString,int);
    void WalkTo(int x, int y);
    QPoint GetMiddleOfBase(QString animKeyword);


    void SetSpecialAnimation(SpecialAnimation, QString animKeyword);
    void DoChoices(Choices choices);
    int GetResult();
    QPoint BaseMiddle();

    void SetTalkingColor(QString color);

    void SetOrderingNumber(int nOrdering);
    int OrderingNumber() const;
    void SetEnableGive(bool);
    bool IsGiveEnabled();
private:
    void UpdateImage();
    void NextInstruction();
    void KillCurrentAnimationAndClearInstructions();
    QString GetCurrentImageFilename() const;
    void LoadObjectProperties();

private:
    XImage m_image;
    int m_top;// needed for scrolling images (ie canoe background)
    int m_left;

    int m_index;// sadly needed for cached zbuffer

    QMap<QString, XImage> m_map;// internal imagecache
    std::deque<QString> m_instructions;

    const QString m_keyword;
    XFrameAnimationKeyword m_fak;// m_keyword, m_currentanimation, m_currentFrame for access control

    bool m_isVisible;
    bool m_isCurrentAnimRepeatingWhilstVisible;
    bool m_isGiveEnabled;
    QString m_currentImageFilename;

    QString m_displayName;
    QString m_talkingColor;
    QString m_talkingAnimation;
    QString m_speech;
    int m_talkingDelay;
    int m_currentTimerId;
    QWidget* m_parent;
    int		m_result;

    // Finally the child objects as in script object model
    QPointer<XAnimations> m_anims;
    QMap<SpecialAnimation, QString> m_specialAnimations;
    IXMainWindowMethodsXRoomNeeds& m_callback;
    int m_orderingNumber;

    friend class XRoomObjects;
    friend class XMainWindow;
    friend class XFolderTraversing;
};

